package com.nervenets.shopping.controller.common;

import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.entity.Selection;
import com.nervenets.general.enumeration.Enums;
import com.nervenets.general.enumeration.Platform;
import com.nervenets.general.i18n.Translator;
import com.nervenets.general.jwt.aspect.JwtSecurity;
import com.nervenets.general.web.BaseController;
import com.nervenets.shopping.enumeration.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "A常量", tags = "A常量")
@RestController
@RequestMapping("/api/constant")
public class ConstantController extends BaseController {
    @ApiOperation(value = "所有")
    @PostMapping({"/all"})
    @JwtSecurity(required = false)
    public ResponseEntity<ResponseResult<HashMap<String, List<Selection>>>> all() {
        HashMap<String, List<Selection>> o = new HashMap<>();

        o.put("Gender", getSelections(Gender.values()));
        o.put("WealthType", getSelections(WealthType.values()));
        o.put("FlowStatus", getSelections(FlowStatus.values()));
        o.put("PayType", getSelections(PayType.values()));
        o.put("AttachmentType", getSelections(AttachmentType.values()));
        o.put("OperateType", getSelections(OperateType.values()));
        o.put("UnionType", getSelections(UnionType.values()));
        o.put("UserType", getSelections(UserType.values()));
        o.put("OrderStatus", getSelections(OrderStatus.values()));
        o.put("DayTime", getSelections(DayTime.values()));
        o.put("Platform", getSelections(Platform.values()));

        return successMessage(o);
    }

    @ApiOperation(value = "性别")
    @PostMapping({"/genders"})
    public ResponseEntity<ResponseResult<List<Selection>>> genders() {
        return enumValues(Gender.values());
    }

    @ApiOperation("资产类型")
    @PostMapping({"/wealthTypes"})
    public ResponseEntity<ResponseResult<List<Selection>>> wealthTypes() {
        return enumValues(WealthType.values());
    }

    @ApiOperation("流水状态")
    @PostMapping({"/flowStatuses"})
    public ResponseEntity<ResponseResult<List<Selection>>> flowStatuses() {
        return enumValues(FlowStatus.values());
    }

    @ApiOperation("支付方式")
    @PostMapping({"/payTypes"})
    public ResponseEntity<ResponseResult<List<Selection>>> payTypes() {
        return enumValues(PayType.values());
    }

    @ApiOperation("附件类型")
    @PostMapping({"/attachmentTypes"})
    public ResponseEntity<ResponseResult<List<Selection>>> attachmentTypes() {
        return enumValues(AttachmentType.values());
    }

    @ApiOperation("操作行为类型")
    @PostMapping({"/operateTypes"})
    public ResponseEntity<ResponseResult<List<Selection>>> operateTypes() {
        return enumValues(OperateType.values());
    }

    @ApiOperation("关联类型")
    @PostMapping({"/unionTypes"})
    public ResponseEntity<ResponseResult<List<Selection>>> unionTypes() {
        return enumValues(UnionType.values());
    }

    @ApiOperation("用户类型")
    @PostMapping({"/userTypes"})
    public ResponseEntity<ResponseResult<List<Selection>>> userTypes() {
        return enumValues(UserType.values());
    }

    @ApiOperation("订单状态")
    @PostMapping({"/orderStatuses"})
    public ResponseEntity<ResponseResult<List<Selection>>> orderStatuses() {
        return enumValues(OrderStatus.values());
    }

    @ApiOperation("每天的时间段")
    @PostMapping({"/dayTimes"})
    public ResponseEntity<ResponseResult<List<Selection>>> dayTimes() {
        return enumValues(DayTime.values());
    }

    private ResponseEntity<ResponseResult<List<Selection>>> enumValues(Enums[] enums) {
        ResponseResult<List<Selection>> responseResult = ResponseResult.success();
        responseResult.setData(getSelections(enums));
        responseResult.setMessage(Translator.translate("code_200"));
        return ResponseEntity.status(HttpStatus.OK).body(responseResult);
    }

    private List<Selection> getSelections(Enums[] enums) {
        return Arrays.stream(enums).map(e -> new Selection(e.getText(), e.getValue())).collect(Collectors.toList());
    }
}
